package com.party.mobile.web.controller.sign;

import com.party.authorization.annotation.Authorization;
import com.party.common.paging.Page;
import com.party.core.model.sign.GroupMember;
import com.party.core.model.sign.SignApply;
import com.party.core.service.sign.ISignApplyService;
import com.party.core.service.sign.ISignRecordService;
import com.party.mobile.biz.currentUser.CurrentUserBizService;
import com.party.mobile.biz.sign.SignMemberNewBizService;
import com.party.mobile.web.dto.AjaxResult;
import com.party.mobile.web.dto.login.output.CurrentUser;
import com.party.mobile.web.dto.sign.output.MyRankOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 个人签到控制器新的
 * Created by yifeng
 *
 * @date 2018/3/5 0005
 * @time 下午 12:21
 */
@Controller
@RequestMapping("sign/member/xcx")
public class SignMemberNewController {
    @Autowired
    private CurrentUserBizService currentUserBizService;
    @Autowired
    private SignMemberNewBizService signMemberBizService;
    @Autowired
    private ISignRecordService signRecordService;
    @Autowired
    private ISignApplyService signApplyService;

    Logger logger = LoggerFactory.getLogger(getClass());


    /**
     * 个人排行榜
     *
     * @param projectId 项目编号
     * @param type      （默认：全部， 0：今天，1：本周，2：本月，3：一月前）
     * @param page      分页参数
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "rank/list")
    public AjaxResult rankListForProject(String projectId, Integer type, Page page) {
        try {
            AjaxResult ajaxResult = new AjaxResult();
            List<GroupMember> list = signMemberBizService.rankList(projectId, type, page);
            ajaxResult.setSuccess(true);
            ajaxResult.setData(list);
            ajaxResult.setPage(page);
            return ajaxResult;
        } catch (Exception e) {
            logger.error("个人排行异常", e);
            return AjaxResult.error();
        }
    }

    /**
     * 个人累计打卡天数
     */
    @ResponseBody
    @RequestMapping(value = "recordCount")
    public AjaxResult recordNum(String applyId) {
        try {
            AjaxResult ajaxResult = new AjaxResult();
            int count = signRecordService.getCount(applyId);
            ajaxResult.setSuccess(true);
            ajaxResult.setData(count);
            return ajaxResult;
        } catch (Exception e) {
            logger.error("获取个人累计打卡天数异常", e);
            return AjaxResult.error();
        }
    }


    /**
     * 我的排行
     *
     * @param projectId 项目编号
     * @param request   请求参数
     * @return 交互数据
     */
    @ResponseBody
    @Authorization
    @RequestMapping(value = "rank/myRank")
    public AjaxResult myRankForProject(String projectId, Integer type, HttpServletRequest request) {
        try {
            AjaxResult ajaxResult = new AjaxResult();
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            MyRankOutput myRankOutput = signMemberBizService.myProjectRank(projectId, type, currentUser);
            ajaxResult.setSuccess(true);
            ajaxResult.setData(myRankOutput);
            return ajaxResult;
        } catch (Exception e) {
            logger.error("我的排行异常", e);
            return AjaxResult.error();
        }
    }

    /**
     * 转发页面我的排行
     *
     * @param
     * @param
     * @return 交互数据
     */
    @ResponseBody
    @Authorization
    @RequestMapping(value = "rank/myRankRedirect")
    public AjaxResult myRankRedirect(String projectId, String memberId, Integer type) {
        try {
            AjaxResult ajaxResult = new AjaxResult();
            CurrentUser currentUser = new CurrentUser();
            currentUser.setId(memberId);
            MyRankOutput myRankOutput = signMemberBizService.myProjectRank(projectId, type, currentUser);
            ajaxResult.setSuccess(true);
            ajaxResult.setData(myRankOutput);
            return ajaxResult;
        } catch (Exception e) {
            logger.error("我的排行异常", e);
            return AjaxResult.error();
        }
    }


    /**
     * 小组成员排行榜
     *
     * @param groupId 小组编号
     * @param page    分页参数
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "group/list")
    public AjaxResult listForGroup(String groupId, Integer type, Page page) {
        try {
            AjaxResult ajaxResult = new AjaxResult();
            GroupMember groupMember = new GroupMember();
            groupMember.setGroupId(groupId);
            List<GroupMember> list = signMemberBizService.groupMemberList(groupMember, type, page);
            ajaxResult.setSuccess(true);
            ajaxResult.setData(list);
            ajaxResult.setPage(page);
            return ajaxResult;
        } catch (Exception e) {
            logger.error("小组成员排行榜异常", e);
            return AjaxResult.error();
        }
    }

    /**
     * 小组成员排行榜
     * ??
     * @param
     * @param page    分页参数
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "group/listRedirect")
    public AjaxResult listForGroupRedirect(String projectId, String memberId, Integer type, Page page) {
        try {
            AjaxResult ajaxResult = new AjaxResult();
            GroupMember groupMember = new GroupMember();
            List<GroupMember> list = new ArrayList<>();
            SignApply signApply = signApplyService.get(memberId,projectId);
            if(null != signApply){
                groupMember.setGroupId(signApply.getGroupId());
                list = signMemberBizService.groupMemberList(groupMember, type, page);
            }
            ajaxResult.setSuccess(true);
            ajaxResult.setData(list);
            ajaxResult.setPage(page);
            return ajaxResult;
        } catch (Exception e) {
            logger.error("小组成员排行榜异常", e);
            return AjaxResult.error();
        }
    }

    /**
     * 我的小组排行
     *
     * @param groupId 小组编号
     * @param request 请求参数
     * @return 交互数据
     */
    @ResponseBody
    @Authorization
    @RequestMapping(value = "group/myRank")
    public AjaxResult myRankForGroup(String groupId, Integer type, HttpServletRequest request) {
        try {
            AjaxResult ajaxResult = new AjaxResult();
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            MyRankOutput myRankOutput = signMemberBizService.myGroupRank(groupId, type, currentUser);
            ajaxResult.setData(myRankOutput);
            ajaxResult.setSuccess(true);
            return ajaxResult;
        } catch (Exception e) {
            logger.error("我的小组排行异常", e);
            return AjaxResult.error();
        }
    }

    /**
     * 转发页面我的小组排行
     *
     * @param
     * @param
     * @return 交互数据
     */
    @ResponseBody
    @Authorization
    @RequestMapping(value = "group/myRankRedirect")
    public AjaxResult myRankForGroupRedirect(String groupId, String memberId,Integer type) {
        try {
            AjaxResult ajaxResult = new AjaxResult();
            CurrentUser currentUser = new CurrentUser();
            currentUser.setId(memberId);
            MyRankOutput myRankOutput = signMemberBizService.myGroupRank(groupId, type, currentUser);
            ajaxResult.setData(myRankOutput);
            ajaxResult.setSuccess(true);
            return ajaxResult;
        } catch (Exception e) {
            logger.error("我的小组排行异常", e);
            return AjaxResult.error();
        }
    }
}
